import { Toast } from 'antd-mobile';
import {
    createSelector,
} from 'reselect'
import {
    reqcartlist,
    reqcartdelete,
    reqcartedit
} from '../../request/api'
const initState = {
    shoplist: [],
    isshow: false
}

const types = {
    CHANGE_SHOPLIST: 'SHOP_CHANGESHOPLIST',
    CHANGE_CHECKED: 'SHOP_CHANGECOUNT',
    CHANGE_ADDNUM: "SHOP_CHANGEADDNUM",
    CHANGE_SUP: 'SHOP_CHANGESUPNUM',
    CHANGE_ALL: 'SHOP_CHANGEALL'
}

const reducer = (state = initState, action) => {
    switch (action.type) {

        case types.CHANGE_SHOPLIST:
            return {
                ...state,
                shoplist: action.arr,
            }

            case types.CHANGE_CHECKED:
                let list = state.shoplist
                list.forEach(item => {
                    if (item.id === action.id) {
                        item.checked = !item.checked
                    }
                })
                state.isshow = list.every(item => {
                    return item.checked
                })
                return {
                    ...state,
                    shoplist: [...list],
                }
                case types.CHANGE_COUNT:
                    return {
                        ...state,
                        count: action.count
                    }
                    // case types.CHANGE_ADDNUM:
                    //     let listADD = state.shoplist
                    //     listADD.forEach(item => {
                    //         if (item.id === action.id) {
                    //             item.num = item.num + 1
                    //         }
                    //     })
                    //     return {
                    //         ...state,
                    //         shoplist: [...listADD]
                    //     }

                        // case types.CHANGE_SUP:
                        //     let listsup = state.shoplist
                        //     listsup.forEach(item => {
                        //         if (item.id === action.id) {
                        //             item.num = item.num - 1
                        //         }
                        //     })
                        //     return {
                        //         ...state,
                        //         shoplist: [...listsup]
                        //     }

                            case types.CHANGE_ALL:
                                let listAll = state.shoplist
                                state.isshow = !state.isshow
                                listAll.map(item => item.checked = state.isshow)
                                return {
                                    ...state,
                                    ishsow: action.isshow,
                                        shoplist: [...listAll]
                                }
                                default:
                                    return state
    }
}

export const actions = {
    changeshoplist: arr => ({
        type: types.CHANGE_SHOPLIST,
        arr
    }),
    changeOne: (id) => ({
        type: types.CHANGE_CHECKED,
        id
    }),
    changeall: (isshow) => ({
        type: types.CHANGE_ALL,
        isshow
    }),
    changeadd: (id) => (dispatch, getState) => {
        reqcartedit({
            id,
            type: 2
        }).then(res => {
            dispatch(actions.reqshoplistnew())
        })
    },
    changesup: (id) => (dispatch, getState) => {
        let shop = getState().shop.shoplist.find(item => item.id === id)
        if (shop.num <= 1) {
            Toast.info('不能在减少了')
            return false;
        }
        reqcartedit({
            id,
            type: 1
        }).then(res => {
            dispatch(actions.reqshoplistnew())
        })
    },


    reqshoplistnew: () => (dispatch, getState) => {
        let shoplist = [...getState().shop.shoplist]
        // console.log(shoplist);
        reqcartlist({
            uid: getState().login.info.uid
        }).then(res => {
            let list = res.data.list[1].content ? res.data.list[1].content : [];
            list.forEach(item => {
                item.checked = shoplist.find((i) => i.id === item.id) ?
                    shoplist.find((i) => i.id === item.id).checked :
                    false;
            })
            dispatch(actions.changeshoplist(list))
        })
    },

    reqshoplist: () => (dispatch, getState) => {
        // let shoplist = [...getState().shop.shoplist]
        reqcartlist({
            uid: getState().login.info.uid
        }).then(res => {
            let list = res.data.list[1].content ? res.data.list[1].content : [];
            list.map(item => (item.checked = false))
            dispatch(actions.changeshoplist(list))
        })
    },


    reqdel: (id) => (dispatch, getState) => {
        reqcartdelete({
            id: id
        }).then(res => {
            dispatch(actions.reqshoplistnew())
        })
    }
}

//导出数据
export const getshoplist = state => state.shop.shoplist
export const getall = state => state.shop.isshow

export const getprice = createSelector(
    [getshoplist],
    (shoplist) => {
        return shoplist.reduce((val, item) => (item.checked ? (val += item.price * item.num) : val), 0)
    }
)
export default reducer